package com.example.redis.pubsub;

import org.apache.catalina.core.ApplicationContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Configuration extends Properties {
    ApplicationContext context;

    private static final long serialVersionUID = 5097570643600785641L;
    private static Configuration instance = null;

    public static synchronized Configuration getInstance() {
        if (instance == null) {
            instance = new Configuration();
        }
        return instance;
    }

    public String getProperty(String key, String defaultValue) {
        String val = getProperty(key);
        return (val == null || val.isEmpty()) ? defaultValue : val;
    }

    public String getString(String name, String defaultValue) {
        return this.getProperty(name, defaultValue);
    }

    public int getInt(String name, int defaultValue) {
        String val = this.getProperty(name);
        return (val == null || val.isEmpty()) ? defaultValue : Integer.parseInt(val);
    }

    public long getLong(String name, long defaultValue) {
        String val = this.getProperty(name);
        return (val == null || val.isEmpty()) ? defaultValue : Integer.parseInt(val);
    }

    public float getFloat(String name, float defaultValue) {
        String val = this.getProperty(name);
        return (val == null || val.isEmpty()) ? defaultValue : Float.parseFloat(val);
    }

    public double getDouble(String name, double defaultValue) {
        String val = this.getProperty(name);
        return (val == null || val.isEmpty()) ? defaultValue : Double.parseDouble(val);
    }

    public byte getByte(String name, byte defaultValue) {
        String val = this.getProperty(name);
        return (val == null || val.isEmpty()) ? defaultValue : Byte.parseByte(val);
    }

    public Configuration() {

//        InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream("myconfig.xml");//通过类加载器来获取
////        InputStream in =  Configuration.class.getClassLoader().getSystemClassLoader().getResourceAsStream("myconfig.xml");//通过当前类加载器的getResourceAsStream方法获取
////        InputStream in = ClassLoader.getSystemResourceAsStream("myconfig.xml");//通过类加载器来获取
//        try {
//            this.loadFromXML(in);
//            in.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        try {
//            InputStream ins = ClassLoader.getSystemClassLoader().getResourceAsStream("application-dev.properties");
//            InputStream ins =  Configuration.class.getClassLoader().getResourceAsStream("application-dev.properties");
                InputStream ins = ClassLoader.getSystemResourceAsStream("application-dev.properties");
            this.load(ins);
        } catch (IOException e) {
            e.printStackTrace();
        }

//        System.out.println(this.getClass().getResource("/").getPath());

    }
}